// pages/healthCareSeach/healthCareSeach.js
var app = getApp();
var fileFunc = require('../../utils/main.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    userInfo: '',
    imageUrl: app.globalData.imageUrl,
    tabData: [
      { txt: '个人', current: true },
      { txt: '单位', current: false }
    ],
    isHotel: false,
    actiItem: [],
    shopList:[],
    focus: false,
    seachValue: '',
    loadMoreTxt: '正在加载数据。。。',
    loadMore: false,
    hasData: true,
    page:1,
    shopHasData:true,
    shopPage:1 ,
    acitem:'',
    current:0,
    currentType:{},
    typeData:[
      {txt:'单位',checked:true},
      { txt: '商家', checked: false, shopType:'0' },
      { txt: '企业', checked: false, shopType: '1' }
    ],
    typeShow:false,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      wx.showLoading({})
      var item = JSON.parse(options.item);
      wx.setNavigationBarTitle({
        title: item.name
      });
      this.setData({
        userInfo: JSON.parse(wx.getStorageSync('userInfo')),
        actiItem: [],
        shopList:[],
        acitem: item
      })
      if (item.type){
        this.setData({
          isHotel:true
        })
        this.getShop(true);
      }
      this.getPersonal(true);
      
  },
  selType:function(e){
    this.data.typeData.forEach(function(ele){
      ele.checked=false;
    })
    this.data.typeData[e.currentTarget.dataset.index].checked=true;
    this.setData({
      typeData: this.data.typeData,
      typeShow:false,
      currentType: this.data.typeData[e.currentTarget.dataset.index], 
      ['tabData[1].txt']:this.data.typeData[e.currentTarget.dataset.index].txt
    })
    this.getShop(true);
  },
  hideMask:function(e){
    this.setData({
      typeShow: false
    })
  },
  getPersonal(flag) {
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'tGyIndustryResource/queryPersonsById',
      data: { 
        type: this.data.acitem.id, 
        param: this.data.seachValue, 
        userId: this.data.userInfo.id, 
        currentPage: this.data.page, 
        pageNumber: 10 
      },
      // method:'POST',
      // header: {
      //   'content-type': 'application/x-www-form-urlencoded' // 默认值
      // },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.data.list.length < 10) {
          that.setData({
            hasData: false
          })
        } else {
          that.setData({
            hasData: true
          })
        }
        if (flag) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          that.setData({
            actiItem: []
          });
        } else {
          that.setData({
            loadMore: false
          });
        }
        that.data.actiItem = that.data.actiItem.concat(res.data);
        that.setData({
          actiItem: res.data.data.list
        })
      }
    })
  },
  getShop(flag) {
    var _this = this;
      var param = {
        id: this.data.acitem.id,
        businessId: this.data.acitem.businessId,
        status: 2,
        param: this.data.seachValue,
        userId: this.data.userInfo.id,
        currentPage:this.data.shopPage,
        pageNumber:10
      };
      if (this.data.currentType.shopType){
        param.shopType = this.data.currentType.shopType;
      }
      wx.request({
        url: app.globalData.httpApiCar + 'tGyIndustryResource/querybusinessByName',
        data: param,
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          if (res.data.data.pp.length < 10) {
            _this.setData({
              shopHasData: false
            })
          } else {
            _this.setData({
              shopHasData: true
            })
          }
          if (flag) {
            wx.hideNavigationBarLoading() //完成停止加载
            wx.stopPullDownRefresh() //停止下拉刷新
            _this.setData({
              shopList: []
            });
          } else {
            _this.setData({
              loadMore: false
            });
          }
          if(flag){
            _this.setData({
              shopList: []
            });
          }
          if (res.data.data){
            for (var j = 0; j < res.data.data.pp.length; j++) {
              _this.data.shopList.push(res.data.data.pp[j])
            }
          }
          
          _this.setData({
            shopList: _this.data.shopList
          })
        }
      })
  },
  // 打开商家页面
  openIndex(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "/pages/shopIndex/shopIndex?id=" + id
    })
  },
  showDeatil:function(e){
    wx.navigateTo({
      url: '/pages/personalProfile/personalProfile?id='+e.currentTarget.dataset.item.id
    })
  },
  swichTab:function(e){
    this.data.tabData.forEach(function(ele){
      ele.current=false;
    })
    this.data.tabData[e.currentTarget.dataset.value].current = true;
    this.setData({
      tabData: this.data.tabData,
      current: e.currentTarget.dataset.value,
      seachValue: '',
      typeShow: e.currentTarget.dataset.value == 1?true:false
    })
  },
  inputFocus: function (e) {
    this.setData({
      focus: true,
      typeShow:false
    })
  },
  inputBlur: function (e) {
    this.setData({
      focus: false
    })
  },
  changeInput: function (e) {
    this.setData({
      seachValue: e.detail.value
    })
    if (this.data.current == 0) {
      this.getPersonal(true);
    }else{
      this.getShop(true);
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载
    if (this.data.current == 0) {
      this.getPersonal(true);
    } else {
      this.setData({
        shopPage:1
      })
      this.getShop(true);
    }
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (this.data.current == 1) {
      if (that.data.shopHasData) {
        this.setData({
          loadMoreTxt: '正在加载数据。。。',
          loadMore: true,
          page: this.data.shopPage + 1
        });
        this.getShop();
      }
    }else{
      if (that.data.hasData) {
        this.setData({
          loadMoreTxt: '正在加载数据。。。',
          loadMore: true,
          page: this.data.page + 1
        });
        this.getPersonal();
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})